﻿/*
* ---------------------------------------------------
* 作者: MTQ 
* 日期： 2021/5/7 9:22:01 
* ---------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MTQ.Model;
using MTQ.BLL;
using MTQ.IBLL;
using MTQ.Common;
using System.IO;




namespace MTQ.Web.Controllers
{

    public class Test_AutoCodeController : BaseController
    {

        private ITest_AutoCodeService itest_autocodeService = new Test_AutoCodeService();


        //                                           
        // GET: //         
        [AccountAuth(Roles = "Test_AutoCode_Index")]
        public ActionResult Index()
        {
            return View();
        }


        //[AccountAuth(Roles = "Test_AutoCode_Index")]      
        public ActionResult Search()
        {


            ServertLayuiTable<Test_AutoCode> servertBootStrapTable = new ServertLayuiTable<Test_AutoCode>();
            GetSearchData(ref servertBootStrapTable, true);


            return Json(servertBootStrapTable, JsonRequestBehavior.AllowGet);
        }

        private void GetSearchData(ref ServertLayuiTable<Test_AutoCode> servertBootStrapTable, bool page)
        {
            Test_AutoCodeQuery itest_autocodeQuery = new Test_AutoCodeQuery();
            itest_autocodeQuery.Searchkey = GetRequestQueryString("keyword");

            //itest_autocodeQuery.UserControlProjectNumber = UserControlProjectNumber;
            if (page)
            {
                servertBootStrapTable = itest_autocodeService.LoadLayuiSearchData(itest_autocodeQuery, Request);
            }
            else
            {
                IQueryable<Test_AutoCode> itest_autocode = itest_autocodeService.GetIQueryable(itest_autocodeQuery);
                servertBootStrapTable.data = itest_autocode.ToList();
                servertBootStrapTable.count = itest_autocode.Count();
            }
        }








        //          
        // GET: /Test_AutoCode/Create          
        [AccountAuth(Roles = "Test_AutoCode_Create")]
        public ActionResult Create()
        {
            Test_AutoCode test_autocode = new Test_AutoCode();
            test_autocode.T_ID = System.Guid.NewGuid();
            return View(test_autocode);
        }

        //          
        // POST: /Test_AutoCode/Create          
        [HttpPost]
        [AccountAuth(Roles = "Test_AutoCode_Create")]
        public ActionResult Create(Test_AutoCode test_autocode)
        {
            ModelMessage listMsg = new ModelMessage();
            if (ModelState.IsValid)
            {
                var result = itest_autocodeService.AddEntity(test_autocode);
                listMsg = new ModelMessage("ChangeResult", "m001", result != null ? "保存成功" : "保存失败");
            }
            return Json(listMsg);
        }


        [AccountAuth(Roles = "Test_AutoCode_Edit")]
        public ActionResult Edit(Guid? primarykey)
        {
            Test_AutoCode test_autocode = itest_autocodeService.LoadEntities(x => x.T_ID == primarykey).FirstOrDefault();
            if (test_autocode == null)
            {
                return HttpNotFound();
            }
            return View(test_autocode);
        }

        [HttpPost]
        [AccountAuth(Roles = "Test_AutoCode_Edit")]
        public ActionResult Edit(Test_AutoCode formtest_autocode)
        {
            ModelMessage listMsg = new ModelMessage();
            if (ModelState.IsValid)
            {
                try
                {
                    Test_AutoCode test_autocode = itest_autocodeService.LoadEntities(x => x.T_ID == formtest_autocode.T_ID).First();
                    test_autocode.T_ID = formtest_autocode.T_ID;
                    test_autocode.T_Name = formtest_autocode.T_Name;
                    test_autocode.T_Int = formtest_autocode.T_Int;
                    test_autocode.T_IntNonull = formtest_autocode.T_IntNonull;
                    test_autocode.T_Date = formtest_autocode.T_Date;
                    test_autocode.T_DateNonull = formtest_autocode.T_DateNonull;
                    test_autocode.T_DateTime = formtest_autocode.T_DateTime;
                    test_autocode.T_DateTimeNonull = formtest_autocode.T_DateTimeNonull;
                    test_autocode.T_Varchar = formtest_autocode.T_Varchar;
                    test_autocode.T_Nvarchar = formtest_autocode.T_Nvarchar;
                    test_autocode.T_Text = formtest_autocode.T_Text;
                    bool result = itest_autocodeService.UpdateEntity(test_autocode);
                    listMsg = new ModelMessage("ChangeResult", "m001", result ? "保存成功" : "保存失败");
                }
                catch (Exception e)
                {
                    listMsg = new ModelMessage("ChangeResult", "m002", "保存失败");
                }
            }
            return Json(listMsg);

        }

        [HttpPost]
        [AccountAuth(Roles = "Test_AutoCode_Edit")]
        public ActionResult ActivateSwich(string primarykeys, string activate = "N")
        {
            ModelMessage modelMessage = new ModelMessage();

            if (OperatorAvailable(primarykeys, activate))
            {
                modelMessage.Code = "m001";
                modelMessage.Message = "修改成功";
            }
            else
            {
                modelMessage.Code = "m002";
            }
            return Json(modelMessage, JsonRequestBehavior.AllowGet);
        }

        private bool OperatorAvailable(string primarykeys, string available)
        {
            List<Guid> listTest_AutoCodeGUID = primarykeys.Split(',').ToListGuid();
            List<Test_AutoCode> listTest_AutoCode = itest_autocodeService.LoadEntities(x => listTest_AutoCodeGUID.Contains(x.T_ID)).ToList();
            bool updaterow = itest_autocodeService.DeleteListEntity(listTest_AutoCode);
            if (updaterow)
            {
                return false;
            }
            return true;
        }


        public ActionResult Export() //List<FieldTitle> fieldtitle
        {

            var fieldtitle = GetRequestQueryString("fieldtitle");
            var listFieldTitle = JsonSerializerHelper.GetJsonList<FieldTitle>(fieldtitle);

            ServertLayuiTable<Test_AutoCode> servertBootStrapTable = new ServertLayuiTable<Test_AutoCode>();
            GetSearchData(ref servertBootStrapTable, false);
            List<ExcelColumn> columnInfo = new List<ExcelColumn>();

            if (listFieldTitle != null)
            {
                foreach (var item in listFieldTitle)
                {
                    columnInfo.Add(new ExcelColumn(item.field, item.title));
                }
            }

            // columnInfo.Add(new ExcelColumn("T_ID", "T_ID"));
            //columnInfo.Add(new ExcelColumn("T_Name", "T_Name"));
            //columnInfo.Add(new ExcelColumn("T_Int", "T_Int"));
            //columnInfo.Add(new ExcelColumn("T_IntNonull", "T_IntNonull"));
            //columnInfo.Add(new ExcelColumn("T_Date", "T_Date"));
            //columnInfo.Add(new ExcelColumn("T_DateNonull", "T_DateNonull"));
            //columnInfo.Add(new ExcelColumn("T_DateTime", "T_DateTime"));
            //columnInfo.Add(new ExcelColumn("T_DateTimeNonull", "T_DateTimeNonull"));
            //columnInfo.Add(new ExcelColumn("T_Varchar", "T_Varchar"));
            //columnInfo.Add(new ExcelColumn("T_Nvarchar", "T_Nvarchar"));
            //columnInfo.Add(new ExcelColumn("T_Text", "T_Text"));
            byte[] btyBytes = null;
            var filename = Server.MapPath("/") + @"DownloadDocument\" + System.Guid.NewGuid().ToString() + ".xlsx";
            NPIOExportExcel.ExportExcelTest<Test_AutoCode>(servertBootStrapTable.data, filename, ref btyBytes, columnInfo, servertBootStrapTable.data.Count);
            return File(filename, "text/comma-separated-values", "file" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");

        }

        public ActionResult UploadFile(FormCollection form)
        {
            string msg = string.Empty;
            if (Request.Files.Count == 0)
            {
                msg = "请选择文件";
                return Json(msg);
            }
            var file = Request.Files[0];
            if (file.ContentLength == 0)
            {
                msg = "请选择文件";
                return Json(msg);
            }
            file = Request.Files[0];

            string virtualPath = Server.MapPath("/") + ("Attachment\\");
            Directory.CreateDirectory(virtualPath);
            string filename = System.Guid.NewGuid().ToString();//取得文件名字                                   
            string path = virtualPath + filename;//获取存储的目标地址                                   
            if (file.FileName.EndsWith(".xls"))
            {
                path += ".xls";
            }
            else if (file.FileName.EndsWith(".xlsx"))
            {
                path += ".xlsx";
            }
            else
            {
                msg = "只能上传Excel格式";
                return Json(msg);
            }
            file.SaveAs(path);

            var result = Test_AutoCodeService.ReadExcel(path, CurrentUserInfo.User_Id, ref msg);
            msg = result ? "成功" : "失败, " + msg;
            return Json(new { success = result, Message = msg });
        }












    }

}


